// tbkPackage/pages/commission/commission.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        choose_nav: 1,
        tbkopen: 1,
        orderList: [],
        billList: [],
        page: 1
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        var that = this;
        var identify;
        //0团员、1团长
                var color, text;
        if (options.identify == "leader") {
            //团长身份进入
            identify = 1;
            color = "#FF983A";
            text = "返佣";
        } else if (options.identify == "user") {
            identify = 0;
            color = "#47B34F";
            text = "返利";
        } else {
            wx.showModal({
                title: "提示",
                content: "身份异常，请重新进入",
                showCancel: false,
                success() {
                    wx.navigateBack({});
                }
            });
        }
        that.setData({
            identify: identify,
            color: color
        });
        that.getBase();
        that.orderList(identify);
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        var that = this;
        that.orderList(that.data.identify, that.data.page);
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //全局基础配置信息，这个模块暂时只用头部颜色，新版无头的也可能不用了
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {}
        });
    },
    //订单列表
    orderList: function(identify, page) {
        var that = this;
        var user_id = wx.getStorageSync("user").uid;
        common.request({
            endpoint: "Taoke/common/promotion-user",
            method: "post",
            data: {
                user_id: user_id,
                identify: identify
            },
            success: res => {
                that.setData({
                    commission: res.extra
                });
            }
        });
        var page = page ? page : 1;
        //团长请求订单列表
                if (identify == 1) {
            common.request({
                endpoint: "Taoke/common/order-list",
                method: "post",
                data: {
                    leader_id: user_id,
                    page: page
                },
                success: res2 => {
                    var orderList = [];
                    orderList = that.data.page == 1 ? res2.extra : orderList.concat(res2.extra);
                    that.setData({
                        orderList: orderList,
                        page: that.data.page + 1
                    });
                }
            });
        } else {
            common.request({
                endpoint: "Taoke/common/order-list",
                method: "post",
                data: {
                    user_id: user_id,
                    page: page
                },
                success: res2 => {
                    var orderList = [];
                    orderList = that.data.page == 1 ? res2.extra : orderList.concat(res2.extra);
                    that.setData({
                        orderList: orderList,
                        page: that.data.page + 1
                    });
                }
            });
        }
    },
    choose_nav: function(e) {
        var id = parseInt(e.currentTarget.dataset.id);
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        this.setData({
            choose_nav: id
        });
        if (id == 1) {
            that.orderList(that.data.identify);
        } else {
            common.request({
                endpoint: "/finance/brokerage_apply",
                method: "post",
                data: {
                    openid: openid,
                    identity: 1,
                    channel: 1
                },
                success: res => {
                    that.setData({
                        billList: res.bill
                    });
                }
            });
        }
    }
});